package org.example.blogserver.dao.impl;

import org.example.blogserver.dao.ArticleDao;
import org.example.blogserver.entity.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ArticleDaoImpl implements ArticleDao {

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public ArticleDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public int addArticle(Article article) {
        String sql = "INSERT INTO blog.ms_article (title, summary, comment_counts, view_counts, like_counts, is_public, author_account, body, create_date) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql, article.getTitle(), article.getSummary(), article.getCommentCounts(),
                article.getViewCounts(), article.getLikeCounts(), article.isPublic(),
                article.getAuthorAccount(), article.getBody(), article.getCreateDate());
    }

    @Override
    public int deleteArticleById(Long id) {
        String sql = "DELETE FROM ms_article WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public int updateArticle(Article article) {
        String sql = "UPDATE ms_article SET title = ?, summary = ?, comment_counts = ?, view_counts = ?, like_counts = ?, " +
                "is_public = ?, author_account = ?, body = ?, create_date = ? WHERE id = ?";
        return jdbcTemplate.update(sql, article.getTitle(), article.getSummary(), article.getCommentCounts(),
                article.getViewCounts(), article.getLikeCounts(), article.isPublic(),
                article.getAuthorAccount(), article.getBody(), article.getCreateDate(),
                article.getId());
    }

    @Override
    public Article findArticleById(Long id) {
        String sql = "SELECT * FROM ms_article WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(Article.class));
    }

    @Override
    public List<Article> findArticlesByAuthorAccount(String authorAccount) {
        String sql = "SELECT * FROM ms_article WHERE author_account = ?";
        return jdbcTemplate.query(sql, new Object[]{authorAccount}, new BeanPropertyRowMapper<>(Article.class));
    }

    @Override
    public List<Article> findArticlesByIsPublic(boolean isPublic) {
        String sql = "SELECT * FROM ms_article WHERE is_public = ?";
        return jdbcTemplate.query(sql, new Object[]{isPublic}, new BeanPropertyRowMapper<>(Article.class));
    }

    @Override
    public int addLike(Long id) {
        String sql = "UPDATE ms_article SET like_counts = like_counts + 1 WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }
    @Override
    public int addView(Long id) {
        String sql = "UPDATE ms_article SET view_counts = view_counts + 1 WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }
}
